package com.school.empsys.controller;

import com.school.empsys.constants.ResponseState;
import com.school.empsys.dto.EmpSalaryDTO;
import com.school.empsys.dto.ResponseInfo;
import com.school.empsys.model.EmpAttendance;
import com.school.empsys.model.EmpSalary;
import com.school.empsys.page.table.PageTableHandler;
import com.school.empsys.page.table.PageTableHandler.CountHandler;
import com.school.empsys.page.table.PageTableHandler.ListHandler;
import com.school.empsys.page.table.PageTableRequest;
import com.school.empsys.page.table.PageTableResponse;
import com.school.empsys.service.EmpAttendanceService;
import com.school.empsys.service.EmpSalaryService;
import com.school.empsys.utils.UserUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 工资接口
 */
@RestController
@RequestMapping("/empSalary")
public class EmpSalaryController {

    private static final Logger logger = LoggerFactory.getLogger(EmpSalaryController.class);

    @Autowired
    EmpSalaryService empSalaryService;

    /**
     * 工资列表
     *
     * @param request
     * @return
     */
    @GetMapping
    @RequiresPermissions("salary:query")
    public PageTableResponse list(PageTableRequest request) {
        Map<String, Object> params = request.getParams();
        UserUtil.setParams(params);
        return new PageTableHandler(new CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return empSalaryService.count(params);
            }
        }, new ListHandler() {
            @Override
            public List<EmpSalary> list(PageTableRequest request) {
                List<EmpSalary> list = empSalaryService.list(params, request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }

    /**
     * 保存工资
     * @param param
     * @return
     */
    @PostMapping
    @RequiresPermissions("salary:add")
    public ResponseInfo save(@RequestBody EmpSalary param) {
        try {
            empSalaryService.save(param);
        } catch (Exception e) {
            logger.error("save Exception", e);
            return ResponseInfo.FAILURE.message("保存失败");
        }
        return ResponseInfo.SUCCESS;
    }

    @GetMapping("/{id}")
    @RequiresPermissions("salary:query")
    public EmpSalary findOne(@PathVariable Integer id) {
        return empSalaryService.findOne(id);
    }

    @PostMapping(value = "/init")
    @RequiresPermissions("salary:query")
    public ResponseInfo init(@RequestBody EmpSalary param) {
        try {
            EmpSalary empSalary = empSalaryService.init(param);
            return ResponseInfo.SUCCESS.data(empSalary);
        } catch (Exception e) {
            logger.error("init Exception", e);
            return ResponseInfo.FAILURE.message("初始化工资失败");
        }
    }

    @GetMapping("/mySalary")
    @RequiresPermissions("salary:query")
    public ResponseInfo mySalary() {
        try {
            EmpSalaryDTO param = new EmpSalaryDTO();
            param.setUserId(UserUtil.getCurrentUser().getId().intValue());
            List<EmpSalary> empSalaryList = empSalaryService.selectByEmpSalaryDTO(param);
            return ResponseInfo.SUCCESS.data(empSalaryList);
        } catch (Exception e) {
            logger.error("mySalary Exception", e);
            return ResponseInfo.FAILURE.message("查询工资失败");
        }
    }
}
